//
//  YXHomeViewController.m
//  CaoShi
//
//  Created by aa on 2018/4/20.
//  Copyright © 2018年 杨旭. All rights reserved.
//

#import "YXHomeViewController.h"
#import "HomeMenuView.h"
#import <SDCycleScrollView.h>
#import "YXWebViewController.h"
#import "YXHomeViewModel.h"
@interface YXHomeViewController ()<UITableViewDelegate,UITableViewDataSource,SDCycleScrollViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic ,strong) SDCycleScrollView *cycleScrollView;
@property (nonatomic ,strong) HomeMenuView *menuView;
@property (nonatomic ,strong) UIButton *cityBtn;
@property (nonatomic ,strong) UIButton *searchBtn;
@property (weak, nonatomic) UIImageView *bgImg;
@property (weak, nonatomic) UIVisualEffectView *effectView;

@property (nonatomic ,strong) NSArray *bannerArr;

@property (nonatomic ,assign) BOOL isEmpty;

@property (nonatomic ,strong) WZHNavigationController *nav;
@property (nonatomic ,strong) YXHomeViewModel *viewModel;

@end

@implementation YXHomeViewController

- (NSArray *)bannerArr {
    if (!_bannerArr) {
        _bannerArr = @[@"banner01",@"banner02",@"banner03",@"banner04"];
    }
    return _bannerArr;
}


- (void)viewDidLoad {
    [super viewDidLoad];

    self.w_barBgAlpha = 0;
    AdjustsScrollViewInsetNever(self, self.tableView);
    _isEmpty = NO;
    [self setupNavBar];
    [self createTableViewUI];
    
//    [self setViewModel:[YXHomeViewModel model]];
//    [_viewModel loadData:self.tableView];
//    [_viewModel cellWithTableView:self.tableView];
//    [_viewModel addRefrsh:self.tableView];
//
//    [_viewModel setNav:[[WZHNavigationController alloc] init]];
//    [_viewModel setBgImg:self.bgImg];
//    [_viewModel setEffectView: self.effectView];
//    [_viewModel setCityBtn: self.cityBtn];
//    [_viewModel setSearchBtn:self.searchBtn];
//    [_viewModel pushSupController:self];

    
}

- (void)createTableViewUI {
    
    _tableView.delegate =self;
    _tableView.dataSource = self;
    _tableView.backgroundColor = [UIColor clearColor];
    
    UIImageView *bgImg = [[UIImageView alloc] initWithFrame:CGRectMake(0, -5, kScreenWidth + 10, kScreenWidth / 16 * 9 + 10)];
    bgImg.image = [UIImage imageNamed:@"banner01"];
    bgImg.centerX = kScreenWidth / 2;
    bgImg.contentMode = UIViewContentModeScaleAspectFill;
    [self.view insertSubview:bgImg atIndex:0];
    self.bgImg = bgImg;

    UIBlurEffect *effect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
    UIVisualEffectView *effectView = [[UIVisualEffectView alloc] initWithEffect:effect];
    effectView.frame = bgImg.bounds;
    [bgImg addSubview:effectView];
    self.effectView = effectView;
    
  
    UIView *headerView = [[UIView alloc] initWithFrame:(CGRectMake(0, 0, kScreenWidth, 380))];
    headerView.backgroundColor = [UIColor py_colorWithHexString:@"#f2f2f2"];
    self.tableView.tableHeaderView = headerView;
    
    _cycleScrollView = [SDCycleScrollView cycleScrollViewWithFrame:(CGRectMake(0, 0, kScreenWidth, 200)) delegate:self placeholderImage:[UIImage imageNamed:@""]];
    _cycleScrollView.localizationImageNamesGroup = self.bannerArr;
    [headerView addSubview:_cycleScrollView];
    
    
    NSArray *titleArr = @[@"房屋租售",@"旅游景点",@"律师咨询",@"车房共享",@"家政服务",@"汽车买卖",@"房屋装修",@"导游接地"];
    NSArray *imageArr = @[@"home_fragment_one",@"home_fragment_two",@"home_fragment_three",@"home_fragment_three",@"home_fragment_five",@"home_fragment_six",@"home_fragment_seven",@"home_fragment_eight"];
    self.menuView = [[HomeMenuView alloc] initWithMenuViewFrame:(CGRectMake(0, 200, kScreenWidth, 180)) withTitle:titleArr withImage:imageArr];
    [headerView addSubview:self.menuView];
    
    
    
    YXWeakSelf
    self.menuView.buttonClicked = ^(NSInteger index) {
     
        if (![YXUserInfoManager isLoad]) {
            [ToastUtils show:@"登录后完成此操作！"];
            [YXUserInfoManager changeRootViewColToLoginVC];
            return;
        }
        
        
        NSLog(@"%ld",index);
        switch (index) {
            case 1000:
            {
                YXWebViewController *vc = [[YXWebViewController alloc] init];
                vc.url = @"https://www.jianshu.com/p/f70dc3e0f436";
                vc.hidesBottomBarWhenPushed = YES;
                [weakSelf.navigationController pushViewController:vc animated:YES];
            }
                break;
            case 1001:
            {

            }
                break;
            case 1002:
            {
                
            }
                break;
            case 1003:
            {
                
            }
                break;
            case 1004:
            {
                
            }
                break;
            case 1005:
            {
                
            }
                break;
            case 1006:
            {
                
                
            }
                break;
            case 1007:
            {
                NSLog(@"更多");
            }
                break;
            default:
                break;
        }
        
    };
    
    UIButton *bennerBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    bennerBtn.frame = CGRectMake(0, 380, kScreenWidth, 50);
    [bennerBtn setBackgroundImage:[UIImage imageNamed:@"banner_activity"] forState:(UIControlStateNormal)];
    [headerView addSubview:bennerBtn];
    
}

- (void)setupNavBar
{
    UIButton *cityBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    [cityBtn setImage:[UIImage imageNamed:@"home_address"] forState:(UIControlStateNormal)];
    [cityBtn setTitle:@"郑州" forState:(UIControlStateNormal)];
    [cityBtn setTitleColor:[UIColor orangeColor] forState:(UIControlStateNormal)];
    [self.w_topView addSubview:cityBtn];
    self.cityBtn = cityBtn;
    [cityBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo (self.w_topView.mas_centerY);
        make.left.mas_equalTo(self.w_topView.mas_left).offset(15);
        [cityBtn sizeToFit];
    }];
    
    UIButton *searchBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    [searchBtn setBackgroundColor:[[UIColor py_colorWithHexString:@"#f3f3f3"] colorWithAlphaComponent:0.3]];
    searchBtn.layer.masksToBounds = YES;
    searchBtn.layer.cornerRadius = 5;
    [searchBtn setTitle:@"搜索" forState:(UIControlStateNormal)];
    [searchBtn setTitleColor:color_textOne forState:(UIControlStateNormal)];
    searchBtn.titleLabel.font = [UIFont systemFontOfSize:12.0f];
    [self.w_topView addSubview:searchBtn];
    self.searchBtn = searchBtn;
    [searchBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo (self.w_topView.mas_centerY);
//        make.centerX.mas_equalTo(self.w_topView.mas_centerX);
        make.left.mas_equalTo(cityBtn.mas_right).offset(15);
        make.width.offset(240);
        make.height.offset(30);
    }];

}


#pragma mark - UITableView Delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 6;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellID = @"homeCellID";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:(UITableViewCellStyleSubtitle) reuseIdentifier:cellID];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    cell.imageView.image = [UIImage imageNamed:@""];
    cell.textLabel.text = @"标题";
    cell.detailTextLabel.text = @"副标题";
    return cell;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat offsetY = scrollView.contentOffset.y;
    CGFloat alpha = offsetY / 100;
//    [self.navBar setBackgroundImage:[UIImage imageWithColor:[[UIColor py_colorWithHexString:@"#F4153D"] colorWithAlphaComponent:alpha] size:CGSizeMake(kScreenWidth, 64)] forBarMetrics:UIBarMetricsDefault];
    self.w_barBgAlpha = alpha;
    
    self.w_navBackgroundColor = [UIColor whiteColor];
    if (offsetY > 100) {
        [self.searchBtn setBackgroundColor:[[UIColor py_colorWithHexString:@"#444444"] colorWithAlphaComponent:0.3]];
    }else {
        [self.searchBtn setBackgroundColor:[[UIColor py_colorWithHexString:@"#f3f3f3"] colorWithAlphaComponent:0.3]];
    }
    
    CGFloat H = kScreenWidth / 16 * 9 + 10 - offsetY;
    
    if (offsetY > 0) {
        H = kScreenWidth / 16 * 9 + 10;
        
        // 设置模糊效果滚动起始位置
        CGRect rect = self.bgImg.frame;
        rect.origin.y = -offsetY;
        self.bgImg.frame = rect;
    }
    if (offsetY < 0){
        self.w_navBarAlpha = 0;
    }else {
        self.w_navBarAlpha = 1;
    }
    
        
//    self.bgImg.frame = CGRectMake(-5, -5, kScreenWidth + 10, H);
    self.effectView.frame = self.bgImg.bounds;
}

 
 
/** 点击图片回调 */
- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index {
    
    
}

// 滚动到第几张图回调
- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didScrollToIndex:(NSInteger)index
{
    NSString *url = self.bannerArr[index];
    [self.bgImg sd_cancelCurrentAnimationImagesLoad];
    [self.bgImg setImage:[UIImage imageNamed:url]];
//    [self.bgImg sd_setImageWithURL:[NSURL URLWithString:url]];

}


@end
